package com.zbkj.front.controller.publicly.device;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.zbkj.common.config.AliMqttConfig;
import com.zbkj.common.exception.AppException;
import com.zbkj.common.response.ZfbMonitorControlLightResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.device.DeviceService;
import com.zbkj.service.service.device.MqttConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * MQTT测试 控制器
 * +----------------------------------------------------------------------
 * | 作者: xiaolic
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/public/mqtt/test")
@Api(tags = "MQTT测试")
public class MqttTestController {

    @Autowired
    private DeviceService deviceService;
    
    @Autowired
    private MqttConfigService mqttConfigService;

    @Autowired
    private AliMqttConfig aliMqttConfig;

    @ApiOperation(value = "发送MQTT消息", notes = "发送MQTT消息") //配合swagger使用
    @PostMapping(value = "/send/msg")
    public CommonResult<ZfbMonitorControlLightResponse> controlLight(@RequestBody JSONObject jsonObject) {
        String topic = jsonObject.getString("topic");
        String msg = jsonObject.getString("msg");
        log.info("发送MQTT消息,topic={},msg={}", topic, msg);
        if (StrUtil.isBlank(topic) || StrUtil.isBlank(msg)) {
            new AppException("参数错误");
        }
        try {
            mqttConfigService.publish(aliMqttConfig.clientId3, topic, msg);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed();
        }
        return CommonResult.success();
    }

}
